CGL.gl.font = {
	Font : function() {
		var Font = {
			image : new Array(),
			loaded : new Array(),
			glyphs : new Array(),
			
			load : function(src) {
				CGL.ajax.loadXML(src, this.loadConfig);
			},
			
			loadConfig : function(xhr) {
				Font.config = xhr.responseXML;
				Font.parseConfig();
			},
			
			parseConfig : function() {
				var pages = Font.config.getElementsByTagName("page");
				var chars = Font.config.getElementsByTagName("char");
				
				for(var i = 0; i < pages.length; i++) {
					var id = pages[i].getAttribute("id");
					var file = pages[i].getAttribute("file");
					this.image[id] = new CGL.gl.sprite.Sprite();
					this.image[id].load("font/" + file);
				}
				
				for(var i = 0; i < chars.length; i++) {
					var id = chars[i].getAttribute("id");
					var x = parseInt(chars[i].getAttribute("x"));
					var y = parseInt(chars[i].getAttribute("y"));
					var width = parseInt(chars[i].getAttribute("width"));
					var height = parseInt(chars[i].getAttribute("height"));
					var xoffset = parseInt(chars[i].getAttribute("xoffset"));
					var yoffset = parseInt(chars[i].getAttribute("yoffset"));
					var xadvance = parseInt(chars[i].getAttribute("xadvance"));
					var page = parseInt(chars[i].getAttribute("page"));
					
					this.glyphs[id] = new CGL.gl.font.Glyph(x, y, width, height, xoffset, yoffset, xadvance, page);
				}
			},
			
			drawString : function(str, x, y) {
				var currentX = x;
				var currentY = 0;
				for(var i = 0; i < str.length; i++) {
					var id = str.charCodeAt(i);
					if(this.glyphs[id] != undefined) {
						this.image[this.glyphs[id].page].renderSub(currentX + this.glyphs[id].xoffset, y + this.glyphs[id].yoffset, this.glyphs[id].x, this.glyphs[id].y, this.glyphs[id].width, this.glyphs[id].height);
						currentX += this.glyphs[id].xadvance;
					}
				}
			},
			
			getInfo : function(x, y, str) {
				var currentX = 0;
				
				for(var i = 0; i < str.length; i++) {
					var id = str.charCodeAt(i);
					var u1 = this.glyphs[id].x / 256;
					var u2pixels = this.glyphs[id].x + this.glyphs[id].width;
					var u2 = u2pixels / 256;
					CGL.util.printLn("renderSub(" + currentX + ", " + y + ", " + this.glyphs[id].x + ", " + this.glyphs[id].y + ", " + this.glyphs[id].width + ", " + this.glyphs[id].height + ")");
					CGL.util.printLn("U1: " + u1 + " U2: " + u2);
					CGL.util.printLn("U2 Pixels: " + u2pixels);
				}
			}
		};
		return Font;
	},

	Glyph : function(x, y, width, height, xoffset, yoffset, xadvance, page) {
		var Glyph = {
			x : x,
			y : y,
			xoffset : xoffset,
			yoffset : yoffset,
			width : width,
			height : height,
			xadvance : xadvance,
			page : page
		};
		return Glyph;
	}
};